export class Sess{
    constructor(){
        this.name = ""
        this.valid()
    }

    valid(){
        // 检查 sessionStorage 中是否存在 "user name"
        const userName = sessionStorage.getItem("chat-user");
        if (!userName) {
            
            
            return false
        } else {
            // 如果有，将用户名保存到 cookie 中
            const oneMonth = 30 * 24 * 60 * 60 * 1000; // 一个月的有效期
            const expireDate = new Date(Date.now() + oneMonth);
            document.cookie = `user_name=${userName};expires=${expireDate.toUTCString()};path=/`;
            this.name = userName
            return true
        }
        
    }

    save(){
        if (this.name.trim() != ""){
            const oneMonth = 30 * 24 * 60 * 60 * 1000; // 一个月的有效期
            const expireDate = new Date(Date.now() + oneMonth);
            document.cookie = `user_name=${this.name};expires=${expireDate.toUTCString()};path=/`;
            sessionStorage.setItem("chat-user", this.name);
        }
    }
}